{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Congestion Charges - Medium"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Prerequesites\n",
    "from pyhive import hive\n",
    "%load_ext sql\n",
    "%sql hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
    "%config SqlMagic.displaylimit = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.\n",
    "List the owners (name and address) of Vehicles caught by camera 1 or 18 without duplication."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "        <th>address</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ambiguous, Arthur</td>\n",
       "        <td>Absorption Ave.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Inconspicuous, Iain</td>\n",
       "        <td>Interception Rd.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Strenuous, Sam</td>\n",
       "        <td>Surjection Street</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Ambiguous, Arthur', 'Absorption Ave.'),\n",
       " ('Inconspicuous, Iain', 'Interception Rd.'),\n",
       " ('Strenuous, Sam', 'Surjection Street')]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT DISTINCT name, address\n",
    "  FROM keeper JOIN vehicle ON (keeper.id=vehicle.keeper) JOIN\n",
    "    image ON (image.reg=vehicle.id)\n",
    "    WHERE camera IN (1, 18)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.\n",
    "Show keepers (name and address) who have more than 5 vehicles."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>name</th>\n",
       "        <th>address</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Ambiguous, Arthur</td>\n",
       "        <td>Absorption Ave.</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Inconspicuous, Iain</td>\n",
       "        <td>Interception Rd.</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Ambiguous, Arthur', 'Absorption Ave.'),\n",
       " ('Inconspicuous, Iain', 'Interception Rd.')]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT name, address\n",
    "  FROM keeper JOIN vehicle ON (keeper.id=vehicle.keeper)\n",
    "    GROUP BY name, address\n",
    "    HAVING COUNT(*)>5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.\n",
    "For each vehicle show the number of current permits (suppose today is the 1st of Feb 2007). The list should include the vehicle.s registration and the number of permits. Current permits can be determined based on charge types, e.g. for weekly permit you can use the date after 24 Jan 2007 and before 02 Feb 2007."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>reg</th>\n",
       "        <th>n_permit</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>SO 02 DSP</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>SO 02 DTP</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>SO 02 JSP</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>SO 02 KSP</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>SO 02 KTP</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>SO 02 QSP</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>SO 02 RSP</td>\n",
       "        <td>1</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('SO 02 DSP', 1),\n",
       " ('SO 02 DTP', 1),\n",
       " ('SO 02 JSP', 1),\n",
       " ('SO 02 KSP', 1),\n",
       " ('SO 02 KTP', 1),\n",
       " ('SO 02 QSP', 1),\n",
       " ('SO 02 RSP', 1)]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "WITH t AS (\n",
    "  SELECT reg, sdate, \n",
    "    CASE WHEN chargetype='Daily' THEN DATE_ADD(sdate, 1)\n",
    "        WHEN chargetype='Weekly' THEN DATE_ADD(sdate, 7)\n",
    "        WHEN chargetype='Monthly' THEN ADD_MONTHS(sdate, 1)\n",
    "        WHEN chargetype='Annual' THEN ADD_MONTHS(sdate, 12)\n",
    "      END AS edate\n",
    "      FROM permit\n",
    ")\n",
    "SELECT reg, COUNT(*) n_permit\n",
    "  FROM t\n",
    "    WHERE '2007-02-01' BETWEEN sdate AND edate\n",
    "    GROUP BY reg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4.\n",
    "Obtain a list of every vehicle passing camera 10 on 25th Feb 2007. Show the time, the registration and the name of the keeper if available."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>reg</th>\n",
       "        <th>whn</th>\n",
       "        <th>keeper</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>SO 02 CSP</td>\n",
       "        <td>2007-02-25 07:45:11.0</td>\n",
       "        <td>Ambiguous, Arthur</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>SO 02 ESP</td>\n",
       "        <td>2007-02-25 18:08:40.0</td>\n",
       "        <td>Ambiguous, Arthur</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('SO 02 CSP', '2007-02-25 07:45:11.0', 'Ambiguous, Arthur'),\n",
       " ('SO 02 ESP', '2007-02-25 18:08:40.0', 'Ambiguous, Arthur')]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "SELECT reg, whn, name keeper\n",
    "  FROM vehicle JOIN keeper ON (vehicle.keeper=keeper.id) JOIN\n",
    "    image ON (vehicle.id=image.reg)\n",
    "    WHERE TO_DATE(whn)='2007-02-25' AND camera=10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5.\n",
    "List the keepers who have more than 4 vehicles and one of them must have more than 2 permits. The list should include the names and the number of vehicles."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * hive://cloudera@quickstart.cloudera:10000/sqlzoo\n",
      "Done.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "    <tr>\n",
       "        <th>keeper</th>\n",
       "        <th>n_vehicle</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "        <td>Inconspicuous, Iain</td>\n",
       "        <td>7</td>\n",
       "    </tr>\n",
       "</table>"
      ],
      "text/plain": [
       "[('Inconspicuous, Iain', 7)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%sql\n",
    "WITH t AS (-- keepers who have more than 2 permits\n",
    "    SELECT vehicle.keeper id, \n",
    "           keeper.name keeper FROM \n",
    "        vehicle JOIN (\n",
    "            SELECT reg FROM permit\n",
    "            GROUP BY reg\n",
    "            HAVING COUNT(*)>2) permit ON (\n",
    "        vehicle.id=permit.reg) JOIN keeper ON (\n",
    "            vehicle.keeper=keeper.id)\n",
    ")\n",
    "SELECT t.keeper, COUNT(*) n_vehicle\n",
    "  FROM vehicle JOIN t ON (\n",
    "      vehicle.keeper=t.id)\n",
    "    GROUP BY t.keeper\n",
    "    HAVING COUNT(*)>4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
